package org.rency.crawler.common.helper;

import java.util.UUID;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.rency.common.utils.security.SHAUtils;

/**
 * 描述: 标识生成帮助类
 * 最近修改时间:2017/8/23 17:21<br>
 *
 * @author CaiyuRen
 * @version V1.0
 * @since 2017/8/23 17:21
 */
public class GeneratorHelper {

  private static final long[] byteTable = createLookupTable();
  private static final long HSTART = 0xBB40E64DA205B064L;
  private static final long HMULT = 7664345821815920749L;

  private static final long[] createLookupTable() {
    long[] byteTable = new long[256];
    long h = 0x544B2FBACAAF1684L;
    for (int i = 0; i < 256; i++) {
      for (int j = 0; j < 31; j++) {
        h = (h >>> 7) ^ h;
        h = (h << 11) ^ h;
        h = (h >>> 10) ^ h;
      }
      byteTable[i] = h;
    }
    return byteTable;
  }
  public static long hashCode(CharSequence cs) {
    long h = HSTART;
    final long hmult = HMULT;
    final long[] ht = byteTable;
    final int len = cs.length();
    for (int i = 0; i < len; i++) {
      char ch = cs.charAt(i);
      h = (h * hmult) ^ ht[ch & 0xff];
      h = (h * hmult) ^ ht[(ch >>> 8) & 0xff];
    }
    return h;
  }

  public static String genHashCode(String... args) {
    HashCodeBuilder builder = new HashCodeBuilder();
    for (String arg : args) {
      builder.append(arg);
    }
    return String.valueOf(builder.toHashCode());
  }

  public static String genHashCode(byte[] txt){
    return SHAUtils.SHA256(txt);
  }

  /**
   * 获取UUID随机数
   * @return
   */
  public static String uuid(){
    return UUID.randomUUID().toString().replaceAll("-","");
  }

}